/*******************************************************************************
 * Copyright (c) 2013 Michele Nanna.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Michele Nanna - initial API and implementation
 ******************************************************************************/
package com.systemfastforward.provinciawifi;

import nl.matshofman.saxrssreader.RssFeed;
import nl.matshofman.saxrssreader.RssItem;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.actionbarsherlock.app.SherlockListFragment;
import com.systemfastforward.provinciawifi.service.DownloadRssFeedTask;
import com.systemfastforward.provinciawifi.ui.adapter.SocialFeedAdapter;

public class TwitterFragment extends SherlockListFragment {
	private static final String TWITTER_RSS_FEED_URI = "http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=provinciawifi";
	private SocialFeedAdapter twitterFeedAdapter;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);		
		new DownloadTwitterFeedtask().execute(TWITTER_RSS_FEED_URI);
		
		getListView().setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				String link = ((RssItem) getListAdapter().getItem(position)).getLink();
				Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
				startActivity(browserIntent);
			}
		});
	}
	
	private class DownloadTwitterFeedtask extends DownloadRssFeedTask {
		
		@Override
		protected void onPostExecute(RssFeed feed) {
			super.onPostExecute(feed);
			if(feed != null){
				twitterFeedAdapter = new SocialFeedAdapter(feed.getRssItems());
			}
			setListAdapter(twitterFeedAdapter);
		}
	}
}
